package com.shadt.pay.wechat.controller;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.shadt.core.controller.BaseController;
import com.shadt.core.util.IpUtil;
import com.shadt.core.util.XMLUtil;
import com.shadt.pay.entity.Pay_Shadt_Order;
import com.shadt.pay.wechat.entity.Pay_WeChat_Preorder;
import com.shadt.pay.wechat.service.WeChatPayService;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.io.xml.XmlFriendlyNameCoder;

/**
 * 微信支付控制器
 * 
 * @author SongR
 * 
 */
@Controller
@RequestMapping("/wechat")
public class WeChatPayController extends BaseController {

	@Autowired
	WeChatPayService wcpoService;
	
	/**
	 * 微信统一支付
	 * 
	 * @param request
	 * @throws Exception
	 */
	@RequestMapping(value = "/unifiedorder/{orderId}")
	public String unifiedOrder(HttpServletRequest request,HttpServletResponse response,@PathVariable String orderId) throws Exception {
		Pay_Shadt_Order order = wcpoService.unifiedOrder(orderId,IpUtil.getIpAddr(request));
		return "redirect:/trade.intercept?tradeno="+order.getOrder_id();
	}
	
	/**
	 * 根据订单号获取app支付所需参数
	 * @param request
	 * @param response
	 * @param orderId
	 * @throws Exception 
	 */
	@RequestMapping(value = "/pay/{orderId}")
	public void getPayInfo(HttpServletRequest request,HttpServletResponse response,@PathVariable String orderId) throws Exception{
		Pay_WeChat_Preorder pwp= wcpoService.getPayInfo(orderId);
		XStream xs = new XStream(new DomDriver("UTF-8",new XmlFriendlyNameCoder("-_", "_")));
		xs.alias("xml", Pay_WeChat_Preorder.class);
		xs.aliasField("package",Pay_WeChat_Preorder.class, "wechat_package");
		xs.omitField(Pay_WeChat_Preorder.class, "orderId");
		xs.omitField(Pay_WeChat_Preorder.class, "creatDate");
		String respXml = "<xml><result_code>ERROR</result_code><msg>订单已关闭</msg></xml>";
		if(pwp!=null)
		 respXml= xs.toXML(pwp);
		try {
			System.out.println(respXml);
			response.getWriter().print(respXml);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 微信通知结果
	 * @param request
	 * @param response
	 * @param inputStream
	 */
	@RequestMapping(value="/notity")
	public void notity(HttpServletRequest request,HttpServletResponse response,ServletInputStream inputStream){
		try {
			String reqXml = XMLUtil.inputStreamToString(inputStream);
			wcpoService.notity(reqXml);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 查询微信通知结果
	 * @param request
	 * @param response
	 * @param inputStream
	 */
	@RequestMapping(value="/orderquery")
	public void orderQuery(HttpServletRequest request,HttpServletResponse response,ServletInputStream inputStream){
		
	}
	
}
